package com.wecan.saasdata.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wecan.api.saasdata.UserData;
import com.wecan.base.PO.User;
import com.wecan.base.constants.SentinelType;
import com.wecan.base.controller.BaseController;
import com.wecan.base.exception.AppException;
import com.wecan.base.model.Result;
import com.wecan.base.utils.AppDateUtils;
import com.wecan.base.utils.AppNumberUtils;
import com.wecan.saasdata.cache.UserCache;
import com.wecan.saasdata.mapper.UserMapper;
import com.wecan.saasdata.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author liuzhihao
 * @Date 2022.01.11
 * @Version 1.0.0
 * @Description 用户数据服务层
 */
@RestController
@RequestMapping(value = "/user")
public class UserController extends BaseController implements UserData {

    @Autowired
    private UserService userService;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserCache userCache;

    public User insert(@RequestBody User user) {
        userMapper.insert(user);
        return user;
    }

    public Integer updateById(@RequestBody User user) {
        int rows = userMapper.updateById(user);
        if (rows > 0) {
            User tempUser = userService.queryUserByPhone(user.getPhone());
            // 这里什么意思
        }
        return userMapper.updateById(user);
    }

    public List<Integer> loadUserRoles(@RequestParam("userId") Long userId) {
        User user = userMapper.selectById(userId);
        String role = user.getRoleIds();
        List<Integer> roles = JSONObject.parseArray(role, Integer.class);
        return roles;
    }

    public User selectById(@RequestParam("id") String id) {
        return userMapper.selectById(id);
    }

    public User queryUserByPhone(@RequestParam("phone") String phone) {
        return userService.queryUserByPhone(phone);
    }

    public Page<User> selectPage(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize, @RequestBody User user) {
        Page<User> page = new Page<>(pageNum, pageSize);
        return userService.selectPage(page, user);
    }

    public boolean updateInfoById(@RequestBody User user) {
        int rows = userMapper.updateById(user);
        return rows > 0;
    }

    public User queryUserById(@RequestParam("id") Long id) {
        return userMapper.selectById(id);
    }

    public boolean editPasswordbyPhone(@RequestParam("phone") String phone, @RequestParam("password") String password) {
        return userService.editPasswordbyPhone(phone, password);
    }

    public User queryUserByNameAndPass(@RequestParam("userName") String userName, @RequestParam("password") String password) {
        return userService.queryUserByNameAndPass(userName, password);
    }

    public User queryNormalUserByPhone(@RequestParam("phone") String phone) {
        return userService.queryNormalUserByPhone(phone);
    }

    public User queryUser(@RequestBody User user) {
        return userService.queryUser(user);
    }

    @Override
    public boolean hasUser(@RequestParam("id") Long id) {
        User user = userMapper.selectById(id);
        return user != null;
    }

    @Override
    public boolean updatePassword(@RequestParam("id") Long id, @RequestParam("password") String password) {
        User user = new User();
        user.setId(id);
        user.setPassword(password);
        int row = userMapper.updateById(user);
        return row > 0;
    }

    @Override
    public String queryPassword(@RequestParam("userId") Long userId) {
        User user = userMapper.selectById(userId);
        return user.getPassword();
    }
}
